<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Uploader</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Uploader</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
   <p>The YUI Uploader leverages Flash to provide file upload functionality beyond the native browser-based methods. Specifically, the Uploader allows for:
	<ol>
		<li> Multiple file selection in a single "Open File" dialog.</li>
		<li> File extension filters to facilitate the user's selection.</li>
		<li> Progress tracking for file uploads.</li>
		<li> A range of available file metadata: filename, size, date created, date modified, and author.</li>
		<li> A set of events dispatched on various aspects of the file upload process: file selection, upload progress, upload completion, data return, and upload errors.</li>
		<li> Inclusion of additional data in the file upload POST request.</li>
		<li> Faster file upload on broadband connections (due to the modified SEND buffer size).</li>
	</ol>
	
	<p><strong>Important usage notes:</strong></p>
	<ul>
	<li>Because of security requirements of the Flash Player, the Uploader has to receive a direct user input in order to initiate file browsing.</li>
	<li>Because of limitations of the Flash Player, the Uploader does not transmit session data in the header of the POST requests it sends to the server. In order to transmit the session data, the developer will need to programmatically extract it from the DOM and send as part of the body of the POST request.</li>
  	<li>The Uploader SWF should always be served from an HTTP server due to Flash Player's restricted local security model.</li>
    <li>The Uploader SWF should not be placed in a container with <code>visibility</code> set to <code>hidden</code>, or <code>display</code> set to <code>none</code>, whether at initialization of the page, or at any subsequent time. Because of how Flash Player is instantiated, setting these properties to these values will result in the loss of communication between the Flash player and the DOM. If it's necessary to hide the instance of the Uploader on the page, its size should be set to 0x0</li>
  	<li>The Uploader requires Flash Player 9.0.45 or higher, with Flash Player 10.1 preferred. The latest version of Flash Player is available at the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</li>
    <li>Because of a bug in IE6, IE7 and IE8, the Uploader SWF does not function properly when loaded from local cache. For that reason, in IE6, IE7 and IE8, it is best to prevent cache loading of the SWF. See the examples for one possible workaround of this issue.
  </ul>

</p>

</div>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for Uploader and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;uploader&#x27;, function (Y) {
    &#x2F;&#x2F; Uploader is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>
				
			
				<h2 id="using-the-uploader">Using the Uploader</h2>
			
				<p>In this section, we'll describe how to use the uploader in detail. First, let's look at the structure of the uploader module under the hood.</p>
				

				<h3 id="anatomy-of-the-uploader">Anatomy of the Uploader</h3>

                <h4 id="hybrid-structure">Hybrid structure</h4>

                <p>The Uploader uses the native functionality of the widely adopted Adobe Flash player to provide methods for sending
				   multiple files to the server and tracking the progress of the uploads. In order to control the Flash player,
				   the Uploader uses the Flash player's built-in <code>ExternalInterface</code> class for communicating with JavaScript.
				   <code>ExternalInterface</code> allows JavaScript to call exposed methods on an instance of a Flash player, and allows
				   the instance of the Flash player to call arbitrary global methods in the global JavaScript space.
				</p>
				<p>
				   In order to properly instantiate the Flash player and communicate with it, the Uploader uses YUI SWF utility. The SWF utility
				   encapsulates the instance of the Flash player and standardizes communication with it (e.g., all Flash player method calls are
				   wrapped in SWF's <code>callSWF</code> function; correspondingly, all method calls from the Flash player are exposed as SWF's events.)
                </p>
				<h4 id="presence-in-the-dom">Presence in the DOM</h4>
				<p>
				   Because of security restrictions of the Flash player, browsing for files cannot be initialized via a method call or a simulated
				   event. Such initialization must come from a hardware device controlled by the user (mouse or keyboard). As a consequence, the instance
				   of the Flash player tied to a specific instance of the Uploader must be present on the page that uses it and must be clickable. Clicking
				   this instance of the Flash player always triggers the standard OS "Browse" dialog (unless the Uploader has been specifically <code>disable()</code>ed.)</p>
				<p>The Uploader provides two methods for displaying its instance of the Flash Player: either as a Flash-based "button" with a developer-provided 			     
				skin Sprite, or as a fully transparent overlay that can be placed above another UI element to mimic native behavior. When the user interacts with
				the Uploader Flash Player instance, all mouse events are bubbled up to JavaScript and are dispatched by the instance of the Uploader.</p>                

				<h3 id="instantiating-and-configuring-the-uploader">Instantiating and Configuring the Uploader</h3>

				<p>To put the Uploader on the page, simply create a new instance of <code>Y.Uploader</code> and provide it with the reference to the container in which it should be placed:</p>

<pre class="code prettyprint">YUI({...}).use(&#x27;uploader&#x27;,function (Y) {

    &#x2F;&#x2F; Default everything
    var uploader = new Y.Uploader({boundingBox:&quot;#uploaderContainerID&quot;});

    &#x2F;&#x2F; Use a custom button skin:
    var uploader = new Y.Uploader({
	    boundingBox:&#x27;#uploaderContainerID&#x27;,
        buttonSkin : &#x27;assets&#x2F;buttonSkin.jpg&#x27;
    });

    &#x2F;&#x2F; Use a custom SWF URL:
    var uploader = new Y.Uploader({
	    boundingBox:&#x27;#uploaderContainerID&#x27;,
        swfURL : &#x27;assets&#x2F;uploader.swf&#x27;
    });

});</pre>

<p>The only required configuration attribute for the Uploader is the <code>boundingBox</code> property, which specifies the container where the Uploader will be rendered. By default, the Uploader is assigned the same dimensions as its container, is transparent, and allows the user to select a single file with any extension. Below are the three other configuration attributes available for Slider:</p>
                <table>
                <thead>
                    <tr>
                        <th>Property</th>
                        <th>Description</th>
                        <th>Default</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><code>buttonSkin</code></td>
                        <td>Specifies an image Sprite to use as the skin for the Uploader.</td>
                        <td>N/A</td>
                    </tr>
                    <tr>
                        <td><code>swfURL</code></td>
                        <td>An alternative path to the Flash SWF file used by the uploader</td>
                        <td>YUI CDN location</td>
                    </tr>
                    <tr>
                        <td><code>transparent</code></td>
                        <td>Whether to render the Uploader SWF as transparent or opaque (useful if keyboard access is needed)</td>
                        <td>true</td>
                    </tr>
                </tbody>
                </table>
<p>Because the Flash player runs in a thread separate from JavaScript, the Uploader methods and properties will not set correctly until the initialization event is fired. Make sure to subscribe to that event and perform subsequent method calls inside its handler:</p>


<pre class="code prettyprint">uploader.on(&quot;uploaderReady&quot;, setupUploader);

var setupUploader = function (event) {
	&#x2F;&#x2F; Uploader method calls &#x2F; property setting here.
}</pre>

<h3 id="file-extension-filtering-and-multiple-file-selection">File extension filtering and multiple file selection</h3>
<p>While the browse dialog in the Uploader is standard for the user's specific operating system, there are two parameters that you can modify to control the user's experience. The first parameter is <code>fileFilters</code>, which allows you to specify file extensions that can be selected. The second parameter is a Boolean <code>multiFiles</code>, which specifies whether the user can select many files, or just one. These parameters are set as follows:</p>

<pre class="code prettyprint">uploader.set(&quot;multiFiles&quot;, true);

var fileFilters = [{description:&quot;Images&quot;, extensions:&quot;*.jpg;*.png;*.gif&quot;},
                   {description:&quot;Videos&quot;, extensions:&quot;*.avi;*.mov;*.mpg&quot;}]; 

    newuploader.set(&quot;fileFilters&quot;, fileFilters);</pre>

<p>Note that even though file extensions are filtered, the user can circumvent this filter by entering "*.*" in the text field of the "Browse" dialog. Therefore, it is up to the developer to check that the files that were selected do, in fact, have correct extensions</p>

<h3 id="the-upload-process">The upload process</h3>
<p> After the user has selected a file (or files) to be uploaded, the selected file list is returned as an array in the <code>fileSelect</code> event. At that point, you can either have the user initialize the actual upload process, or initialize it automatically, without any additional user action.</p>
<p> Each selected file is uploaded in its own POST request. You can either upload each file individually, and manage the queue using your own code, or use the Uploader's built-in queue management. To upload a single file, the following method call is made:</p>

<pre class="code prettyprint">uploader.upload(&quot;file0&quot;, &quot;upload.php&quot;);</pre>

<p> To upload all files using the built-in queue manager, you can call the <code>uploadAll()</code> method:</p>
<pre class="code prettyprint">uploader.uploadAll(&quot;upload.php&quot;);</pre>

<p> You can also upload a subset of the provided files, using an overloaded call to upload(): </p>
<pre class="code prettyprint">uploader.upload([&#x27;file0&#x27;, &#x27;file1&#x27;], &quot;upload.php&quot;);</pre>

<p> To monitor the upload progress, you can subscribe to the <code>uploadprogress</code> event, which reports the number of bytes uploaded, and the number of total bytes that needs to be uploaded:</p>

<pre class="code prettyprint">uploader.on(uploadprogress, reportProgress);

function reportProgress (event) {
	Y.log(&quot;Percent of &quot; + event.id + 
              &quot; uploaded: &quot; + 
              Math.round((100 * event.bytesLoaded &#x2F; event.bytesTotal)) 
              + &quot;%&quot;);
}</pre>

                <h3 id="uploader-events">Uploader events</h3>
                <p>Uploader fires the following events during operation:</p>
                <table>
                <thead>
                    <tr>
                        <th>Event</th>
                        <th>When</th>
                        <th>Payload</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><code>uploaderReady</code></td>
                        <td>Uploader ready to accept method calls</td>
                        <td><code>None</code></td>
                    </tr>
                    <tr>
                        <td><code>fileselect</code></td>
                        <td>The user has selected some files to upload</td>
                        <td><code>{ fileList : {fileID: {id:String, name:String, size:Number, date:Number}, ...} }</code></td>
                    </tr>
                    <tr>
                        <td><code>uploadstart</code></td>
                        <td>A specific file's upload has started.</td>
                        <td><code>{id : String}</code></td>
                    </tr>
                    <tr>
                        <td><code>uploadprogress</code></td>
                        <td>TCP socket reports how many bytes of a specific file have been uploaded</td>
                        <td><code>{id:String, bytesLoaded:Number, bytesTotal:Number}</code></td>
                    </tr>
                    <tr>
                        <td><code>uploadcomplete</code></td>
                        <td>A specific file has been uploaded to the server.</td>
                        <td><code>{id:String}</code></td>
                    </tr>
                    <tr>
                        <td><code>uploadcompletedata</code></td>
                        <td>The server has returned data in response to the upload POST request.</td>
                        <td><code>{id:String, data:String}</code></td>
                    </tr>

                </tbody>
                </table>

                <p>This is not an exhaustive list.  See the <a href="http://yuilibrary.com/yui/docs/api/module_uploader.html">API docs</a> for a complete listing.</p>
</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using-the-uploader">Using the Uploader</a>
<ul class="toc">
<li>
<a href="#anatomy-of-the-uploader">Anatomy of the Uploader</a>
<ul class="toc">
<li>
<a href="#hybrid-structure">Hybrid structure</a>
</li>
<li>
<a href="#presence-in-the-dom">Presence in the DOM</a>
</li>
</ul>
</li>
<li>
<a href="#instantiating-and-configuring-the-uploader">Instantiating and Configuring the Uploader</a>
</li>
<li>
<a href="#file-extension-filtering-and-multiple-file-selection">File extension filtering and multiple file selection</a>
</li>
<li>
<a href="#the-upload-process">The upload process</a>
</li>
<li>
<a href="#uploader-events">Uploader events</a>
</li>
</ul>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="How to upload a single file, while tracking progress and using a sprite-skinned button">
                                        <a href="uploader-simple.html">Simple Uploader with Progress Tracking</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to upload a single file, along with GET and POST Vars Submission and receive data from the server">
                                        <a href="uploader-withvars.html">Single File Upload with Additional Data</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to upload multiple files with progress tracking and a transparent overlay upload button">
                                        <a href="uploader-multiple.html">Multiple Files Upload with Transparent Overlay Button</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
